<?php
if (! defined ( "YUEXUAN_REAL_ROOT_PATH" )) {
	exit ( "Access Denied." );
}
class tagException extends Exception {
	// pass
}

/**
 * 标签
 *
 * @author tongjun
 *
 */
class tally_tag extends base {
	private $tagTableName = "#tablePre#tag";
	public static $tagMappingTableName = "#tablePre#tagMapping";

	// 按用户分页读取标签
	public function findTagByAccountId($userId, $tagName, $showHide = false, $start = 0, $limit = 20) {
		$where = "";
		$limitStr = "";
		if ($tagName) {
			$where .= " and t.name like '%{$tagName}%' ";
		}
		if (! $showHide) {
			$where .= " and t.status=1 ";
		}
		if ($limit > 0) {
			$limitStr = "limit {$start}, {$limit}";
		}
		$tagMappingTableName = self::$tagMappingTableName;
		$sql = "select t.*, count(m.id) as count from {$this->tagTableName} t " . "left join {$tagMappingTableName} m on m.tagId=t.id " . "where t.userId={$userId} {$where} " . "group by t.id order by t.orderIndex asc, t.id asc " . "{$limitStr}";
		return $this->db ( ext_dbHandle::$slaveDbServer )->getRows ( $sql );
	}

	// 按用户统计标签数
	public function countTagByAccountId($userId, $tagName, $showHide = true) {
		$where = "";
		if ($tagName) {
			$where .= " and name like '%{$tagName}%' ";
		}
		if (! $showHide) {
			$where .= " and status=1 ";
		}
		$sql = "select count(1) as c from {$this->tagTableName} " . "where userId={$userId} {$where}";
		$row = $this->db ( ext_dbHandle::$slaveDbServer )->getRow ( $sql );
		return $row ["c"];
	}

	// 根据账号id读取标签
	public function findTagById($userId, $tagId) {
		$sql = "select * from {$this->tagTableName} " . "where userId={$userId} and id={$tagId}";
		return $this->db ( ext_dbHandle::$slaveDbServer )->getRow ( $sql );
	}

	// 根据账号及id列表读取标签
	public function findTagByIds($userId, $tagIds) {
	    $tagIds[] = -1;
	    $tagIds = join(",", $tagIds);
		$sql = "select * from {$this->tagTableName} " . "where userId={$userId} and id in ({$tagIds})";
		return $this->db ( ext_dbHandle::$slaveDbServer )->getRows ( $sql );
	}

	// 创建标签
	public function createTag($userId, $name, $orderIndex = 999) {
		$sql = "select * from {$this->tagTableName} " . "where userId={$userId} and name='{$name}'";
		$oldRow = $this->db ( ext_dbHandle::$slaveDbServer )->getRow ( $sql );
		if ($oldRow) {
			throw new tagException ( "此标签[".$name."]己存在" );
		}
		$defaultTagInfo = array ();
		$tagInfo = array (
				"userId" => $userId,
				"name" => $name,
				"orderIndex" => $orderIndex
		);
		$tagInfo = array_merge ( $defaultTagInfo, $tagInfo );
		$this->db ( ext_dbHandle::$masterDbServer )->insert ( $this->tagTableName, $tagInfo );
		return $this->db(ext_dbHandle::$masterDbServer)->insert_id();
	}

	public function findOrCreateTagId($userId, $name){
		$sql = "select * from {$this->tagTableName} " . "where userId={$userId} and name='{$name}'";
		$oldRow = $this->db ( ext_dbHandle::$slaveDbServer )->getRow ( $sql );
		if ($oldRow) {
			return $oldRow["id"];
		}else{
			return $this->createTag($userId, $name);
		}
	}

	// 更新标签
	public function updateTagInfo($userId, $tagId, $tagInfo) {
		$sql = "select * from {$this->tagTableName} " . "where userId={$userId} and name='{$tagInfo["name"]}' and id<>{$tagId}";
		$oldRow = $this->db ( ext_dbHandle::$slaveDbServer )->getRow ( $sql );
		if ($oldRow) {
			throw new tagException ( "此标签[".$tagInfo["name"]."]己存在" );
		}
		$where = array (
				"id" => $tagId,
				"userId" => $userId
		);
		$this->db ( ext_dbHandle::$masterDbServer )->update ( $this->tagTableName, $tagInfo, $where );
	}

	// 删除标签
	public function removeTag($userId, $tagId) {
		$tagMappingTableName = self::$tagMappingTableName;
		$sql = "delete from {$tagMappingTableName} " . "where tagId in (select id from {$this->tagTableName} " . "where userId={$userId} and id={$tagId})";
		$db = $this->db ( ext_dbHandle::$masterDbServer );
		$db->begin ();
		try {
			$db->exc ( $sql );

			$sql = "delete from {$this->tagTableName} " . "where userId={$userId} and id={$tagId}";
			$db->exc ( $sql );
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollback ();
			throw $e;
		}
	}

	// 删除用户的所有标签
	public function removeTagByUserId($userId) {
		$tagList = $this->findTagByAccountId($userId, null, true, 0, -1);
		if($tagList){
			foreach($tagList as $tag){
				$this->removeTag($userId, $tag["id"]);
			}
		}
	}
}
